package mapdemo;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MapApiDemo {

    public static void main(String[] args) {
        Map map = new TreeMap();
        map.put("S001","张三");
        map.put("S002","李四");
        map.put("S003","王五");
        map.put("S004","赵六");
        map.put("S005","孙琦");

        System.out.println("map length is:"+map.size());

        //遍历方式一：
        Set<Map.Entry> entries = map.entrySet();
        System.out.println("-----------遍历方式一---------");
        for(Map.Entry entry: entries){
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }

        //遍历方式二
        Set keys = map.keySet();
        Iterator it = keys.iterator();
        System.out.println("-----------遍历方式二---------");
        while(it.hasNext()){
            Object key = it.next();
            Object value = map.get(key);
            System.out.println(key + "=" + value);
        }

        //遍历方式三，使用lambda表达式
        System.out.println("-----------遍历方式三---------");
        map.forEach((k,v)->{
            System.out.println(k + "=" + v);
        });

        map.remove("S002");// 删除'S002'键
        System.out.println("-----------删除'S002'后-----------");
        entries = map.entrySet();
        for(Map.Entry entry: entries){
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }

        map.clear();
        System.out.println(map.isEmpty());
    }
}
